{extend name="public/base"/}
{block name="body"}
<div class="main-box no-header clearfix">
	<header class="main-box-header clearfix">
		<div class="pull-left">
			<h2>{$coupon_id|get_table_field="id","name","coupon"}-{$meta_title} </h2>
		</div>
		<div class="pull-right">
			<a href="{:url('addcode',array('id'=>$coupon_id))}" class="btn btn-primary ">  生 成</a>
			<button class="btn btn-danger ajax-post confirm" url="{:url('delcode')}" target-form="ids">删 除</button>
		</div>
	</header>
	<div class="main-box-body clearfix">
		<div class="table-responsive">
      <table class="table table-striped table-hover">
        <thead>
          <tr>
            <th width="30"><input class="checkbox check-all" type="checkbox"></th>
            <th width="60">ID</th>
            <th>微信昵称</th>
            <th>会员名称</th>
            <th>优惠码</th>
            <th>满足金额</th>
            <th>优惠金额</th>
            <th>到期时间</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          {volist name="list" id="item"}
          <tr>
            <td><input class="ids row-selected" type="checkbox" name="id[]" value="{$item['id']}"></td>
            <td>{$item['id']}</td>
            <td>{$item['uid']|get_table_field="id","nickname","WxUser"}</td>
						<td>{$item['uid']|get_table_field="id","name","WxUser"}</td>
            <td>{$item['code']}</td>
            <td>{$item['total']}</td>
            <td>{$item['price']}</td>
            <td>{$item['deadline']}</td>
            <td>{eq name="item['status']" value='1'}
							订单号<a href="{:url('admin/order/edit',array('id'=>$item['order_id']))}">{$item['order_id']|get_table_field="id","out_trade_no","Order"}</a>已使用
							{else /}
							未使用
							{/eq}</td>
            <td>
							{empty name="$item['uid']"}
							<a  href="javascript:;"  data-toggle="modal" data-target="#myModal" data-id="{$item['id']}">绑定用户</a>
							{/empty}
              <a href="{:url('delcode',array('id'=>$item['id']))}" class="confirm ajax-get">删除</a>
            </td>
          </tr>
          {/volist}
        </tbody>
      </table>
			{$list->render()}
		</div>
	</div>
</div>


<!-- Modal -->
<div class="modal fade "  id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">绑定用户</h4>
            </div>

                <div class="modal-body">
                    <div class="input-group">
                        <div class="input-control search-box search-box-circle has-icon-left has-icon-right search-example" id="searchboxExample">
                            <input id="inputSearch"  type="search" class="form-control search-input" placeholder="ID/微信昵称/用户名">
                            <label for="inputSearch" class="input-control-icon-left search-icon"><i class="icon icon-search"></i></label>
                        </div>
                        <span class="input-group-btn btnsearchuser" style="    vertical-align: top;">
                            <button class="btn btn-primary" type="button">搜索</button>
                          </span>
                    </div>
                    <input type="hidden" name="couponid" >


                    <div id="userbox"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary submit-btn-search "  target-form="form-horizontal">确定</button>
                </div>

        </div>
    </div>
</div>
<script src="__PUBLIC__/xsd/js/template.js"></script>

<script id="zt" type="text/html">
    {{each  as value i}}
    <div >
        <label>
            <input type="radio" name="uservalue" value="{{value.id}}"> {{value.id}}/{{value.nickname}}/{{value.name}}
        </label>
    </div>

    {{/each}}
</script>

<script>
    $(function(){
        $('#myModal').on('show.bs.modal', function (e) {
            var id = $(e.relatedTarget.outerHTML).attr('data-id');
            $(e.target).find('[name="couponid"]').val(id);
        })


        $('.btnsearchuser').click(function () {
            var val = $('#inputSearch').val();
            if(val){
                $.get("{:url('getUserList')}",{'val':val},function (data) {


                        var html = template('zt', data.data);
                        $('#userbox').html(html);

                })
            }

        })

        $('.submit-btn-search').click(function () {
            var id = $('[name="uservalue"]:checked').val();
            var couponid = $('[name="couponid"]').val();
            if(id && couponid){
                $.post("{:url('setCustomer')}",{'id':id, 'couponid':couponid},function (data) {


                    updateAlert(data.msg , 'success');

                    if(data.code == 1){
                        setTimeout(function() {
                            location.reload()
                        }, 2000);
                    }
                })
            }
        });
    })
</script>
{/block}
